package com.bitjsc.server;

import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.red5.server.adapter.ApplicationAdapter;
import org.red5.server.api.IClient;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;

import com.bitjsc.server.data.Player;
import com.bitjsc.server.data.Room;
import com.bitjsc.server.data.Table;

public class RedServer extends ApplicationAdapter {

	private static final Log log = LogFactory.getLog(RedServer.class);
	public HashMap<String, Player> players = new HashMap<String, Player>();
	public HashMap<String, Room> rooms = new HashMap<String, Room>();
	
	public boolean checkLogedin(String username)
	{
		// duyet qua  players de? check xem player nay da log in chua
		// neu da login tra ve true
		// neu chua tra ve false
		return false;
		
	}
	public boolean login(IConnection connection, String username,
			String password) {
		Player player = this.getCurrentPlayer(connection);
		if(this.checkLogedin(username))
		{
			
			return false;
		}
		else
		{
			player.setName(username);			
			player.getCurrentRoom().invokeAll("updatePlayerInfo", new Object[]{player.getClientID(),player.getName(),player.getLocation(),player.getMoney()});
			return true;
			
		}
		

	}

	public Player getCurrentPlayer(IConnection connection) {

		IClient client = connection.getClient();
		Player player = players.get(client.getId());
		return player;

	}

	public String joinRoom(IConnection connection, String roomName) {
		if (this.rooms.containsKey(roomName)) {
			Player player = this.getCurrentPlayer(connection);
			this.rooms.get(roomName).addPlayer(player);
			return roomName;
		}
		return "";
	}
	public String getRoomInfo(IConnection connection)
	{
		Player player = this.getCurrentPlayer(connection);
		if(player.getCurrentRoom()!=null)
		{
			Room room = player.getCurrentRoom();
			for ( String key : room.tables.keySet() )
			 {
				Table tmp =  room.tables.get(key);
				for(String key2 : tmp.players.keySet())
				{
					if(key2!=player.getClientID())
					{
						Player tmpPlayer = tmp.players.get(key2);
						
						Object[] result = {tmp.getName(),tmpPlayer.getClientID(),tmpPlayer.getName(),tmpPlayer.getLocation(),tmpPlayer.getMoney()};
						player.invoke("addTablePlayer",result);
					}
				}
			 }
			
		}
		return ""; 
	}
	public String joinTable(IConnection connection, String tableName) {
		Player player = this.getCurrentPlayer(connection);
		
		if (player.getCurrentRoom().tables.containsKey(tableName)) {
			player.getCurrentRoom().tables.get(tableName).addPlayer(player);
			return tableName;
		}
		return "";
	}

	public boolean appConnect(IConnection connection, Object[] params) {
		Player player = new Player(connection);
		this.players.put(player.getClientID(), player);
		
		player.invoke("makePlayerClient", new Object[]{player.getClientID(),player.getName()});
		System.out.println("CLIENT " + player.getName() + " Connected !");
		return true;
	}

	public boolean appStart(IScope app) {
		this.rooms.put("VIP I", new Room("VIP I"));
		this.rooms.put("VIP II", new Room("VIP II"));
		this.rooms.put("VIP III", new Room("VIP III"));
		this.rooms.put("Event I", new Room("Event I"));
		this.rooms.put("Event II", new Room("Event II"));
		this.rooms.put("Event III", new Room("Event III"));
		this.rooms.put("Nomal I", new Room("Nomal I"));
		
		if (super.appStart(app) == false) {
			return false;
		}
		
		return true;
	}

	public void appDisconnect(IConnection conn) {

		System.out.println("Red5First.appDisconnect "
				+ conn.getClient().getId());

		Player player = this.players.get(conn.getClient().getId());
		
		
		if (player.getCurrentRoom() != null) {
			player.getCurrentRoom().removePlayer(player);
			player.getCurrentTable().removePlayer(player);
			

		}
		this.players.remove(conn.getClient().getId());

	}

}
